package com.itlong.cloud.rabbitmq.consumer;

import com.itlong.cloud.POJO.PO.TimeTaskDeadMessageQueuePO;
import com.itlong.cloud.service.ITimeTaskDeadMessageQueueService;
import com.itlong.cloud.utils.node.JsonUtil;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

/**
 * <desc>
 *      死信消息队列。
 * </desc>
 *
 * @createDate 2017/11/16
 */
@Component
public class DeadQueueConsumer {

    private static final Logger LOG = LoggerFactory.getLogger(DeadQueueConsumer.class);

    @Autowired
    ITimeTaskDeadMessageQueueService iTimeTaskDeadMessageQueueService;

    @RabbitListener(queues = "dead")
    public void process(Message message, Channel channel) throws InterruptedException ,IOException {
        try {
            byte[] body = message.getBody();
            //LOG.info("死信队列服务消费 : " + new String(body));
            TimeTaskDeadMessageQueuePO timeTaskDeadMessageQueuePO = JsonUtil.toObject(new String(body),
                    TimeTaskDeadMessageQueuePO.class);
            Date messageTime = timeTaskDeadMessageQueuePO.getCreateTime();
            timeTaskDeadMessageQueuePO.setCreateTime(new Date());
            timeTaskDeadMessageQueuePO.setMessageTime(messageTime);
            iTimeTaskDeadMessageQueueService.save(timeTaskDeadMessageQueuePO);
        } catch (Exception e) {
            LOG.error("【死信消息队列】的timeTaskDeadMessageQueuePO==========>{}",(JsonUtil.toObject(new String(message.getBody()),
                    TimeTaskDeadMessageQueuePO.class)).toString());
            LOG.error("【死信消息队列】：消费失败！！！",e);
        }finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    }

}
